/**
 * This file is part of Idegen, the application to create IDE project files.
 *
 * @copyright Copyright (c) 2011-2012, Walter Tamboer http://waltertamboer.nl
 * @file Idegen/main.cpp
 */

#include "Idegen/PrecompiledHeader.hpp"
#include "Idegen/Application/CommandLineParser.hpp"
#include "Idegen/Application/Application.hpp"
#include "Idegen/IO/FileSystem.hpp"

/**
 * @brief The entry point of this application.
 * @param[in] argc The amount of arguments passed to the startup of the application.
 * @param[in] argv The arguments that are passed to the startup of the application.
 */
int main( int argc, char* argv[] )
{
	int exitCode = 0;

	Idegen::Application* application  = NULL;

	try
	{
		Idegen::CommandLineParser commandLineParser;

		// Add some flags that we allow:
		commandLineParser.AddFlag( "-base", "-b", "The path that should be used as the working directory.", false );
		commandLineParser.AddFlag( "-config", "-c", "The path to the configurations file.", true );
		commandLineParser.AddFlag( "-generator", "-g", "The name of the generator to use. When no name is provided; all generators are used.", false );

		if ( commandLineParser.Parse( argc, argv ) )
		{
			const std::string settingsFile = commandLineParser.GetFlagValue( "-config" );
			const std::string settingsPath = Idegen::GetDirectoryPath( settingsFile );

			application = new Idegen::Application( settingsFile, settingsPath );

			exitCode = ( application->Run() ? 0 : 1 );
		}
		else
		{
			throw std::string( "Usage: app -c \"path to settings xml\"" );
		}
	}
	catch ( std::string& ex )
	{
		std::cout << ex << std::endl;
		exitCode = 1;
	}
	catch ( std::wstring& ex )
	{
		std::wcout << ex << std::endl;
		exitCode = 1;
	}
	
	delete application;

	//std::cin.get();
	return exitCode;
}